$(function(){
    obj.init();
});

var obj = new JsFun();
function JsFun(){
    'use strict';
    return {
        dom: {
            frame: null,
            area: null,
            school: null,
            start: null,
            end: null,
            mode:null
        },
        params: {
            schoolId: null,
            startTime: null,
            endTime: null
        },
        timer: null,
        init: function () {
            this.dom.frame   = $('iframe');
            this.dom.school  = $('#school_id');
            this.dom.area    = $('#school_area');
            this.dom.start   = $('#startTime');
            this.dom.end     = $('#endTime');
            this.dom.mode    = $('#view_mode');
            this.dom.btn     = $('#btn_view');

            this.listener();
        },
        listener: function () {
            var $this = this;
            //点击查询
            $this.dom.btn.click(function () {
                $this.funObj().startCache($this.dom.mode.val());
            });
            //点击查询
            // $('#bt_view_normal').click(function () {
            //     // $this.funObj().viewUsage(false);
            //     $this.funObj().startCache(false);
            // });
            //加载完毕后关闭提示
            $this.dom.frame.load(function(){
                swal.close();
            });
            //反转查看
            // $('#bt_view_reverse').click(function() {
            //     // $this.funObj().viewUsage(true);
            //     $this.funObj().startCache(true);
            // });
            //绑定区域
            $this.funObj().bindAreaSchool();
        },
        funObj: function(){
            var $this = this;
            return {
                // dealParams: function () {
                //     $this.params = {
                //         schoolId: $this.dom.school.val(),
                //         startTime: $this.dom.start.val(),
                //         endTime: $this.dom.end.val()
                //     };
                //     if (!$this.params.schoolId) {
                //         window.top.xhTop.showError('学校为必选项！');
                //         return false;
                //     }
                //     if (!$this.params.startTime || !$this.params.endTime) {
                //         window.top.xhTop.showError('时间不能为空！');
                //         return false;
                //     }
                // },
                startCache: function (reverse) {
                    // $this.funObj().dealParams();
                    $this.params = {
                        schoolId: $this.dom.school.val(),
                        startTime: $this.dom.start.val(),
                        endTime: $this.dom.end.val()
                    };
                    if (!$this.params.schoolId) {
                        window.top.xhTop.showError('学校为必选项！');
                        xhExtra.setBtnNormal($this.dom.btn);
                        return false;
                    }
                    if (!$this.params.startTime || !$this.params.endTime) {
                        window.top.xhTop.showError('时间不能为空！');
                        xhExtra.setBtnNormal($this.dom.btn);
                        return false;
                    }
                    xhExtra.ajaxRequest('Statsschool/startCloudTaskClassCache', $this.params, {check: false, timeout: 2}, function (data) {
                        if (typeof data === 'object' && data.error) {
                            window.top.xhTop.showError(data.error);
                            xhExtra.setBtnNormal($this.dom.btn);
                            return false;
                        }
                        $this.dom.frame.attr('src', '');
                        $this.timer = setInterval(function () {
                            $this.funObj().checkCache(reverse);
                        }, 1000 * 5);
                        setTimeout(function () {
                            // $('#bt_view_usage, #bt_view_reverse').attr('disabled', true);
                            swal({
                                title: '后台统计中！请耐心等待...',
                                // text: '预计时间3分钟...',
                                showConfirmButton: false,
                                type: 'success',
                                allowOutsideClick: false
                            });
                            // setTimeout(function () {
                            //     swal.close();
                            // }, 5 * 1000);
                        });
                    });
                },
                checkCache: function (reverse) {
                    xhExtra.ajaxRequest('Statsschool/checkCloudTaskClassCache', $this.params, {check: false}, function (data) {
                        if (data.code === 200) {
                            clearInterval($this.timer);
                            $this.funObj().viewUsage(reverse);
                            // $('#bt_view_usage, #bt_view_reverse').attr('disabled', false);
                        }
                    });
                },
                viewUsage: function (reverse) {
                    $this.dom.frame.attr('src', '');
                    xhExtra.setBtnNormal($this.dom.btn);
                    setTimeout(function () {
                        // var schoolId    = $this.dom.school.val(),
                        //     startTime   = $this.dom.start.val(),
                        //     endTime     = $this.dom.end.val();
                        // if (!schoolId) {
                        //     xhTop.showError('学校为必选项！');
                        //     return false;
                        // }
                        // if (!startTime || !endTime) {
                        //     xhTop.showError('时间不能为空！');
                        //     return false;
                        // }
                        $this.dom.frame.attr('src', baseUrl + 'Statsschool/cloudTaskClassDetail?' + xhTool.parseParam($.extend({}, $this.params, {reverse: reverse})));
                        // swal({
                        //     title: '统计执行中！请耐心等待...',
                        //     showConfirmButton: false,
                        //     type: 'success',
                        //     allowOutsideClick: false
                        // });
                        // setTimeout(function () {
                        //     swal.close();
                        // }, 30 * 1000);
                    }, 0);
                },
                bindAreaSchool: function () {
                    if ($this.dom.area.length >= 1) {
                        xhExtra.ajaxRequest('Other/getDep', {tag: 'XHYYB', depth: 2}, {check: true}, function (data) {
                            $this.dom.area.select2({data: data}).change(function () {
                                xhExtra.ajaxRequest('Other/getSchoolByDep', {depId: $(this).val()}, {check: false}, function (schools) {
                                    if (xhExtra.checkError(schools)) {
                                        $this.dom.school.empty();
                                        return false;
                                    }
                                    $this.dom.school.empty().select2({data: schools});
                                });
                            }).trigger('change');
                        });
                    }
                    else {
                        $this.dom.school.select2();
                    }
                }
            }
        }
    }
}